package com.icygen.core;

import org.eclipse.ui.console.MessageConsole;
import org.eclipse.ui.console.MessageConsoleStream;
import org.eclipse.ui.console.ConsolePlugin;
import org.eclipse.ui.console.IConsoleManager;
import org.eclipse.ui.console.IConsole;

public class Utils {
	
	private static String CONSOLE_NAME = "Icygen Console";

	public static MessageConsole findConsole(String name) {
		ConsolePlugin plugin = ConsolePlugin.getDefault();
		IConsoleManager conMan = plugin.getConsoleManager();
		IConsole[] existing = conMan.getConsoles();
		for (int i = 0; i < existing.length; i++)
			if (name.equals(existing[i].getName()))
				return (MessageConsole) existing[i];
		//no console found, so create a new one
		MessageConsole myConsole = new MessageConsole(name, null);
		conMan.addConsoles(new IConsole[]{myConsole});
		
		return myConsole;
	}
	
	public static void DebugMessage(String message)
	{
		/*
		 * Once a console is created, you can write to it either by directly modifying its IDocument 
		 * or by opening an output stream on the console. 
		 * This snippet opens a stream and writes some text to a console:
		 */
		
		MessageConsole myConsole = findConsole(CONSOLE_NAME);
		MessageConsoleStream out = myConsole.newMessageStream();
		out.println(message);
		 
		
		
		
		/*
		 * Creating a console and writing to it do not create or reveal the Console view. 
		 * If you want to make that sure the Console view is visible, 
		 * you need to reveal it using the usual workbench API. 
		 * Even once the Console view is revealed, keep in mind that it may contain several pages, 
		 * each representing a different IConsole provided by a plug-in. 
		 * Additional API asks the Console view to display your console. 
		 * This snippet reveals the Console view and asks it to display a particular console instance: 
		 */
		/*	
		IConsole myConsole = findConsole(CONSOLE_NAME);
		IWorkbenchPage page = ...;// obtain the active page
		String id = IConsoleConstants.ID_CONSOLE_VIEW;
		IConsoleView view = (IConsoleView) page.showView(id);
		view.display(myConsole);
		*/
	}
}
